(function ($) {
	$.fn.extend({
		selfFullCalendar: function (options) {
			var settings = $.extend({
				//每个月份的天数
				allMonth: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],

				//星期
				allNameOfWeekDays: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"],

				//月份名
				allNameOfMonths: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],

				//数据
				dataArr: null,

				month: new Date().getMonth() + 1,

				year: new Date().getFullYear(),

				clickMore: function (e) {
					alert(e.target.id);
				},

				clickLink: function (e) {
					alert(e.target.id);
				}

			}, options);
			return this.each(function () {
				newElement('div', [
					['id', 'calender', 1]
				], false, this);
				var newDate = new Date();

				//当前年（4位）
				var yearZero = newDate.getFullYear();

				//当前月
				var monthZero = newDate.getMonth();

				//当前日
				var day = newDate.getDate();
				var currentDay = 0,
					currentDayZero = 0;
				// var month=monthZero, year=yearZero;

				//今天日期 格式：2014-10-10
				var curmon = parseInt(monthZero) + 1;
				var today = yearZero + "-" + curmon + "-" + day;

				//起讫年
				var yearMin = yearZero - 5,
					yearMax = yearZero + 5;
				var target = '';
				var hoverEle = false;

				/*
				创建新元素
				type:类型（div，p...）
				attrs:属性（数组）
				content:内容
				toNode:父节点
				*/
				function newElement(type, attrs, content, toNode) {
					var ele = document.createElement(type);
					if (attrs) {
						for (var i = 0; i < attrs.length; i++) {
							eval('ele.' + attrs[i][0] + (attrs[i][2] ? '=`' : '=') + attrs[i][1] + (attrs[i][2] ? '`' : ''));
						}
					}
					if (content) ele.appendChild(document.createTextNode(content));
					if (toNode) toNode.appendChild(ele);
					return ele;
				}

				//日历控件
				function calender() {
					var parentEle = document.getElementById("calender");

					//判断将要显示日历的div中是否有子节点，如果有，则删除
					while (parentEle.firstChild) parentEle.removeChild(parentEle.firstChild);

					//当月份在1月或者12月时，选择上一个月和下一个月时，月份和年份的变化
					//if(settings.month < 0){settings.month+=12; settings.year-=1;}
					//if(settings.month > 11){settings.month-=12; settings.year+=1;}

					//当年份超出范围时，自动修改最大和最小年
					//if(settings.year==yearMax-1) yearMax+=1;  
					//if(settings.year==yearMin) yearMin-=1;

					//判断是否为闰年
					check();

					//创建选择条件的面板
					var control = newElement('div', [
						['id', 'control', 1]
					], false, parentEle);

					//创建右侧切换页面面板
					var urlDiv = newElement('div', [
						['id', 'urlDiv', 1]
					], false, parentEle);

					//创建显示日历面板
					var displayCalender = newElement('div', [
						['id', 'display_calender', 1]
					], false, parentEle);

					//创建月份切换链接（前一个月）
					//var prevControlPlus=newElement('a', [['href','javascript:void(0);',1], ['className','controlPlus',1],['onclick',function(){prevMonth();}]], false, control);
					//var aImage=newElement('img', [['src','image/previous.png',1]], false, prevControlPlus);


					//创建年份选择下拉菜单
					//var selectYear=newElement('select', [['onchange',function(){setYear(this)}],['className','seleEle',1],['id','sele_year',1]], false, control);  
					//for(var i=yearMin; i<yearMax; i++){
					//newElement('option', [['value',i,1]], i, selectYear);
					//}
					//selectYear.selectedIndex=settings.year-yearMin;

					//创建月份选择下拉菜单
					//var selectMonth=newElement('select', [['onchange',function(){setMonth(this)}],['className','seleEle',1],['id','sele_month',1]], false, control);  
					//for(var i=0; i<settings.allNameOfMonths.length; i++){
					//newElement('option', [['value',i,1]], settings.allNameOfMonths[i], selectMonth);
					//}
					//selectMonth.selectedIndex=settings.month;

					// check();

					//创建月份切换链接（后一个月）
					//var nextControlPlus=newElement('a', [['href','javascript:void(0);',1], ['className','controlPlus',1],['onclick',function(){nextMonth();}]],false, control);
					//aImage=newElement('img', [['src','image/next.png',1]], false, nextControlPlus);

					//每个月1日星期
					var beginDay = new Date(settings.year, settings.month, 1).getDay();
					var dayIndex = new Array();
					for (var i = 0; i < settings.allMonth[settings.month] + beginDay; i++) {
						if (i < beginDay) {
							dayIndex[i] = 0;
						} else {
							dayIndex[i] = i - beginDay + 1;
						}
					}

					//创建显示日期的表格
					var newMonth = newElement('table', [
						['cellSpacing', 0, 1],
						['className', "calender_table", 1]
					], false, displayCalender);

					//显示月份部分
					var newMonthBody = newElement('tbody', false, false, newMonth);

					//星期显示行
					var tr = newElement('tr', [
						['className', 'weekdays', 1]
					], false, newMonthBody);

					//显示周数
					if (settings.showWeek) {
						newElement('th', [
							['className', 'weekname', 1],
							['width', '70px', true]
						], false, tr);
					}

					//显示星期
					for (var j = 0; j < 7; j++) th = newElement('th', [
						['className', 'weekname', 1]
					], settings.allNameOfWeekDays[j], tr);

					var dayLen = dayIndex.length;

					//计算表格的行数
					var rows = Math.ceil(dayLen / 7);

					//把日期放入表格中
					for (var k = 1; k <= rows; k++) {
						var trEle = newElement('tr', false, false, newMonthBody);
						var m = (k - 1) * 7;
						if (settings.showWeek) { //显示周数
							newElement('td', [
								['className', 'weekname', 1]
							], '第 ' + getYearWeek(settings.year, (parseInt(settings.month) + 1), dayIndex[m]) + ' 周', trEle);
						}
						for (var j = 0; j < 7; j++) {
							if (dayIndex[j + m] && dayIndex[j + m] != 0) {
								var idName = 'contentDiv_' + dayIndex[j + m].toString();
								var dateDay = dayIndex[j + m];
								var monthVal = parseInt(settings.month) + 1
								var dayVal = settings.year + "" + monthVal + "" + dateDay;
								var todayVal = today.replace(/-/g, "");
								//todayVal = todayVal.replace("-","");
								var d1 = parseInt(dayVal);
								var d2 = parseInt(todayVal);
								//var d1 = new Date(dayVal);   
								//var d2 = new Date(today);
								if (d1 == d2) {
									//显示日期
									var idNa = 'dateDiv_' + dayIndex[j + m].toString();
									var tdEle = newElement('td', [
										['id', 'today_td', 1]
									], false, trEle);
									var divDateEle = newElement('div', [
										['className', 'dateDiv', 1],
										['id', idNa, 1]
									], dayIndex[j + m], tdEle);

									var divContentEle = newElement('div', [
										['className', 'today', 1],
										['id', idName, 1]
									], false, tdEle);

								} else {
									//显示日期
									var idNa = 'dateDiv_' + dayIndex[j + m].toString();
									var tdEle = newElement('td', false, false, trEle);
									var divDateEle = newElement('div', [
										['className', 'dateDiv', 1],
										['id', idNa, 1]
									], dayIndex[j + m], tdEle);

									var divContentEle = newElement('div', [
										['className', 'contentDiv', 1],
										['id', idName, 1]
									], false, tdEle);
								}
								//if(dataArray.length > 0){}
								addSchedule(dateDay, settings.dataArr, divContentEle, divDateEle);
							} else {
								var tdEle = newElement('td', [
									['className', 'no_thisMon', 1]
								], false, trEle);
							}
						}
					}
				}

				function prevMonth() {
					settings.month--;
					calender();
				}

				function nextMonth() {
					settings.month++;
					calender();
				}

				//添加待办事项
				function addSchedule(dateDay, dataArray, ele, dateEle) {
					var divEle = newElement('div', [
						['className', 'content_div', 1]
					], false, ele);
					var ulEle = newElement('ul', [
						['className', 'text_content', 1]
					], false, divEle);
					if (dataArray.length > 0) {
						for (var i = 0; i < dataArray.length; i++) {
							var monthVal = parseInt(settings.month) + 1;
							var dateDayVal = parseInt(dateDay);
							if (dateDayVal < 10) {
								dateDayVal = "0" + dateDayVal;
							}
							if (monthVal < 10) {
								monthVal = "0" + monthVal;
							}
							var dayVal = settings.year + "-" + monthVal + "-" + dateDayVal;
							var day1 = new Date(dayVal);
							var day2 = new Date(dataArray[i][0]);
							if (Date.parse(day1) == Date.parse(day2)) {
								var tdEle = ele.parentNode;
								tdEle.setAttribute("class", "weekly_td");
								var contentArr = dataArray[i][1];
								var IDArr = dataArray[i][2];
								var BIDArr = dataArray[i][3];
								if (contentArr.length > 0 && contentArr.length <= 3) {
									for (var j = 0; j < contentArr.length; j++) {
										var idName = "linkID_" + IDArr[j] + "_" + BIDArr[j];
										var liEle = newElement('li', [
											['className', 'text_content_li', 1]
										], false, ulEle);
										var aEleA = newElement('a', [
											['id', idName, 1],
											['className', 'text_content_a', 1],
											['href', 'javascript:void(0);', 1],
											['onclick', function (e) {
												settings.clickLink(e);
											}]
										], contentArr[j], liEle);
									}
								} else if (contentArr.length > 3) {
									//					   var moreId="moreID_"+dayVal;
									var moreId = "moreID_" + IDArr[0] + "_" + BIDArr[0];
									var moreDiv = newElement('div', [
										['className', 'more_div', 1],
										['id', moreId, 1],
										['onclick', function (e) {
											settings.clickMore(e);
										}]
									], "", ele);
									for (var j = 0; j < 3; j++) {
										var idName = "linkID_" + IDArr[j] + "_" + BIDArr[j];
										var content = contentArr[j].split("+");
										var liEle = newElement('li', [
											['className', 'text_content_li', 1]
										], false, ulEle);
										var aEle = newElement('a', [
											['id', idName, 1],
											['className', 'text_content_a', 1],
											['href', 'javascript:void(0);', 1],
											['onclick', function (e) {
												settings.clickLink(e);
											}]
										], contentArr[j], liEle);
									}
								}
							}
						}
					}
				}

				//判断当前日期是当年的第几周
				function getYearWeek(year, month, day) {
					var date;
					if (day == 0) {
						date = new Date(year + '-' + month + '-1').getTime() - 24 * 60 * 60 * 1000;
						date = new Date(date);
					} else date = new Date(year + '-' + month + '-' + day);
					var date2 = new Date(date.getFullYear(), 0, 1);
					var day1 = date.getDay();
					var day2 = date2.getDay();
					// var day1 = date.getDay() ? date.getDay() : 7;
					// var day2 = date2.getDay() ? date2.getDay() : 7;
					d = Math.round((date.getTime() - date2.getTime() + (day2 - day1) * (24 * 60 * 60 * 1000)) / 86400000);
					return Math.ceil(d / 7);
					// return Math.ceil(d / 7) + 1;
				}

				//判断年份是否为闰年，如果是闰年，则2月份是29天
				function check() {
					if (settings.year % 4 == 0 && (settings.year % 100 != 0 || settings.year % 400 == 0)) settings.allMonth[1] = 29;
					else settings.allMonth[1] = 28;
				}

				function setMonth(ele) {
					settings.month = parseInt(ele.value);
					calender();
				}

				function setYear(ele) {
					settings.year = parseInt(ele.value);
					calender();
				}
				calender();
			});
		}
	});
})(jQuery);